Tricky multiple transformations that create new dataframe in Python